part of base.temples;

///四种视图状态
enum LoadState { State_Success, State_Error, State_Loading, State_Empty }

///根据不同状态来展示不同的视图
class EmptyWidget extends StatefulWidget {
  final LoadState state; //页面状态
  final Widget? successWidget; //成功视图
  final Widget? errorWidget; //错误
  final Widget? emptyWidget; //为空视图
  final Widget? loadWidget; //为空视图
  final VoidCallback? errorRetry; //错误事件处理

  const EmptyWidget(
      {Key? key,
      this.state = LoadState.State_Loading, //默认为加载状态
      this.successWidget,
      this.errorWidget,
      this.emptyWidget,
      this.loadWidget,
      this.errorRetry})
      : super(key: key);

  @override
  _EmptyWidgetState createState() => _EmptyWidgetState();
}

class _EmptyWidgetState extends State<EmptyWidget> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      //宽高都充满屏幕剩余空间
      width: double.infinity,
      height: double.infinity,
      child: _buildWidget,
    );
  }

  ///根据不同状态来显示不同的视图
  Widget? get _buildWidget {
    switch (widget.state) {
      case LoadState.State_Success:
        return widget.successWidget;
      case LoadState.State_Error:
        return widget.errorWidget ?? _errorView;
      case LoadState.State_Loading:
        return widget.loadWidget ?? _loadingView;
      case LoadState.State_Empty:
        return widget.emptyWidget ?? _emptyView;
      default:
        return null;
    }
  }

  ///加载中视图
  Widget get _loadingView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      decoration: const BoxDecoration(color: Colors.transparent),
      alignment: Alignment.center,
      child: Container(
        height: 80,
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            color: Color(0x88000000), borderRadius: BorderRadius.circular(6)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[CircularProgressIndicator(), Text(StrRes.loading)],
        ),
      ),
    );
  }

  ///错误视图
  Widget get _errorView {
    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          "ic_load_empty"
              .assetImage(width: 80.w, height: 80.h, package: "base"),
          Text(StrRes.load_fail),
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.blue,
            ),
            onPressed: widget.errorRetry,
            child: Text(
              StrRes.reload,
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
    );
  }

  ///数据为空的视图
  Widget get _emptyView {
    return SizedBox(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Placeholder(),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text(StrRes.no_data),
          )
        ],
      ),
    );
  }
}
